import { Destroyable, Event as XrEvent } from "xbsj-base";
import { PSUEViewer } from "./PSUEViewer";

export class PSUEViewerWrapper extends Destroyable {
    private _instance = this.disposeVar(new PSUEViewer(this._container, { url: this._wsuri, esmsgWsUri: this._esmsgWsUri }));

    get webSocketReady() {
        return this._instance.esWebSocketReady;
    }

    private _event = this.disposeVar(new XrEvent<[eventName: string, eventDetailStr: string]>());

    get event() { return this._event; }

    get debug() { return this._instance.debug; }
    set debug(value: boolean) { this._instance.debug = value; }

    constructor(private _container: HTMLDivElement, private _wsuri: string, private _esmsgWsUri: string | undefined) {

        super();
        if ('addEventListener' in this._instance) {
            // const eventNames = [
            //     'error',
            //     'wserror',
            //     'wsclose',
            //     'videoinited',
            //     'ueevent',
            //     'esmsgwsopen',
            //     'esmsgwsclose',
            //     'esmsgwserror',
            //     'disconnected',//ESUEPlayer 断开连接
            //     'webRtcConnected',
            // ];

            const eventNames = [
                "esmsgwsopen",
                'error',
                'videoinited',
                'ueevent',
                'webRtcConnected',
            ];
            eventNames.forEach(eventName => {
                const eventFunc = (event: Event) => {
                    // @ts-ignore
                    this._event.emit(eventName, event.detail as string);
                }
                // @ts-ignore
                this._instance.addEventListener(eventName, eventFunc);
                // @ts-ignore
                this.dispose(() => this._instance.removeEventListener(eventName, eventFunc));
            });

            return;
        }

        // @ts-ignore
        this.dispose(this._instance.errorEvent.disposableOn(e => {
            // @ts-ignore
            this._event.emit('error', e.detail);
        }));
        // @ts-ignore
        this.dispose(this._instance.webRtcConnectedEvent.disposableOn(e => {
            // @ts-ignore
            this._event.emit('webRtcConnected', e.detail);
        }));

        // @ts-ignore
        this.dispose(this._instance.videoinitedEvent.disposableOn(e => {
            // @ts-ignore
            this._event.emit('videoinited', e.detail);
        }));
        // @ts-ignore
        this.dispose(this._instance.ueevent.disposableOn(e => {
            // @ts-ignore
            this._event.emit('ueevent', e.detail);
        }));

        // // @ts-ignore
        // this.dispose(this._instance.closeEvent.disposableOn(e => {
        //     // @ts-ignore
        //     this._event.emit('wsclose', e.detail);
        // }));
        // // @ts-ignore
        // this.dispose(this._instance.esmsgwsopenEvent.disposableOn(e => {
        //     // @ts-ignore
        //     this._event.emit('esmsgwsopen', e.detail);
        // }));
        // // @ts-ignore
        // this.dispose(this._instance.esmsgwserrorEvent.disposableOn(e => {
        //     // @ts-ignore
        //     this._event.emit('esmsgwserror', e.detail);
        // }));
        // // @ts-ignore
        // this.dispose(this._instance.esmsgwscloseEvent.disposableOn(e => {
        //     // @ts-ignore
        //     this._event.emit('esmsgwsclose', e.detail);
        // }));
        // // @ts-ignore
        // this.dispose(this._instance.disconnectedEvent.disposableOn(e => {
        //     // @ts-ignore
        //     this._event.emit('disconnected', e.detail);
        // }));
    }

    emitUIInteractionForBigData(params: any, callback?: (...params: any[]) => void) {
        return new Promise<any[]>((resolve, reject) => {
            const callbackWrapper = (...params: any[]) => {
                resolve(params);
                callback && callback(...params);
            };
            try {
                this._instance.emitUIInteractionForBigData(params, callbackWrapper);
            } catch (error) {
                console.warn(`emitUIInteraction发生错误：${error}`);
                reject(error);
            }
        });
    }

    getVideoSize() {
        return this._instance.getVideoSize();
    }

    resize(width: number, height: number) {
        // this._instance.resize(width, height);
        this._instance.resizeUEVideo(width, height);
    }
}
